package com.ktjy.controller;


import com.ktjy.dto.DeptDTO;
import com.ktjy.dto.MenuDTO;
import com.ktjy.pojo.SysDept;
import com.ktjy.pojo.SysMenu;
import com.ktjy.service.ISysDeptService;
import com.ktjy.service.ISysMenuService;
import com.ktjy.utils.AjaxResult;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 菜单权限表 前端控制器
 * </p>
 *
 * @author jjx
 * @since 2025-07-31
 */
@RestController
@RequestMapping("/api/sys/menu")
public class SysMenuController {
    Logger log= LoggerFactory.getLogger(SysMenuController.class);

    @Resource
    ISysMenuService sysMenuService;

    @GetMapping("/list")
    public AjaxResult list(MenuDTO menuDTO){
        List<SysMenu> list = sysMenuService.findList(menuDTO);
        return AjaxResult.success(list);
    }

    @GetMapping("/treeselect")
    public AjaxResult treeselect() {
        List<SysMenu> list = sysMenuService.list();
        List<SysMenu> depts = sysMenuService.buildTree(list,0l);
        return AjaxResult.success(depts);
    }

    @PostMapping("/save")
    public AjaxResult saveOrUpdate(@RequestBody SysMenu sysMenu){
        try {
            boolean ok=sysMenuService.saveOrUpdate(sysMenu);
            if(ok){
                return AjaxResult.success("保存成功");
            }
        } catch (Exception e) {
            log.error("SysMenuController中saveOrUpdate方法执行报错的。",e);
        }
        return AjaxResult.error("保存失败");
    }

    @GetMapping("/view/{id}")
    public AjaxResult view(@PathVariable("id") Long id){
        //查询用户详情
        try {
            SysMenu sysMenu=sysMenuService.getById(id);
            if(sysMenu!=null){
                return AjaxResult.success(sysMenu);
            }
        } catch (Exception e) {
            log.error("SysMenuController中edit方法执行报错的。",e);
        }
        return AjaxResult.error();
    }

    @DeleteMapping("/del/{id}")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            boolean ok= sysMenuService.removeById(id);
            if(ok){//删除成功
                return AjaxResult.success("删除成功");
            }
        } catch (Exception e) {
            log.error("SysMenuController中del方法执行报错的。",e);
        }
        return AjaxResult.error("删除失败");
    }
}
